package com.prp.gwt.client.widgets.mail;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.mail.MailListener;
import com.prp.gwt.client.vo.MailInfo;

public class ReadOutMailPanel extends VerticalPanel {
	private static ReadOutMailPanel instance;
	private MailInfo mail;

	public static ReadOutMailPanel getInstance() {
		if (instance == null)
			instance = new ReadOutMailPanel();
		return instance;
	}

	public void load(MailInfo mailinfo) {
		mail = mailinfo;
		clear();
		initPanel();
	}

	public void initPanel() {
		add(getMailPanel());
		add(getControlPanel());
	}

	public VerticalPanel getMailPanel() {
		VerticalPanel mailPanel = new VerticalPanel();
		mailPanel.setSpacing(5);
		mailPanel.setSize("100%", "80%");

		HorizontalPanel titlePanel = new HorizontalPanel();

		Label titleLabel = new Label("主   题：");
		titleLabel.setWidth("70");
		titlePanel.add(titleLabel);

		Label titleBox = new Label();
		titlePanel.add(titleBox);
		titleBox.setText(mail.getMailboxInfo().getTitle());
		mailPanel.add(titlePanel);
		
		HorizontalPanel addressPanel = new HorizontalPanel();
		
		Label addressLabel = new Label("收件人：");
		addressLabel.setWidth("70");
		addressPanel.add(addressLabel);
		
		Label addressBox = new Label();
		addressPanel.add(addressBox);
		addressBox.setText(String.valueOf(mail.getMailboxInfo().getReceiver().getId()));
		
		mailPanel.add(addressPanel);
		
		Label contentLabel = new Label("内容：");
		Label topLabel = new Label();
		topLabel.setText("-------------------------------------" + "\n");
		Label textLabel = new Label();
		textLabel.setText(mail.getContent());
		Label botLabel = new Label();
		botLabel.setText("-------------------------------------" + "\n");
		
		mailPanel.add(contentLabel);
		mailPanel.add(topLabel);
		mailPanel.add(textLabel);
		mailPanel.add(botLabel);
		
//		TextArea textBox = new TextArea();
//		textBox.setSize("100%", "179px");
//		
//		mailPanel.add(textBox);

		return mailPanel;
	}

	public HorizontalPanel getControlPanel() {
		HorizontalPanel controlPanel = new HorizontalPanel();
		controlPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		controlPanel.setSpacing(5);
		controlPanel.setWidth("100%");

		Button cancelButton = new Button("New button");
		cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				MailListener.getInstance().cancelMail();
				MailView.getInstance().setBarVisible(true);
				MailView.getInstance().showWidget(MailView.OUTMAILBOX);
			}

		});
		cancelButton.setHeight("25");
		cancelButton.setText("返回");
		controlPanel.add(cancelButton);

		return controlPanel;

	}

}
